﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.DependencyInjection;
using PLCS.Application.Contracts;
using PLCS.Application.EquipmentApp.Hubs;
using PLCS.Application.StationApp.Hubs;
using PLCS.Domain;
using Volo.Abp.Application;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.BackgroundJobs.Quartz;
using System.Collections.Specialized;
using Volo.Abp.Quartz;
using Microsoft.Extensions.Configuration;
using Quartz;
using PLCS.EntityFrameworkCore.EntityFrameworkCore;

namespace PLCS.Application;

[DependsOn(typeof(AbpDddApplicationModule))]
[DependsOn(typeof(PLCSApplicationContractsModule))]
[DependsOn(typeof(PLCSEntityFrameworkCoreModule))]
[DependsOn(typeof(PLCSDomainModule))]
[DependsOn(typeof(AbpAutoMapperModule))]
[DependsOn(typeof(AbpAspNetCoreSignalRModule))]
public class PLCSApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<AbpBackgroundJobWorkerOptions>(options =>
        {
            options.DefaultTimeout = 864000; //10 days (as seconds)
        });
        context.Services.Configure<AbpExceptionHandlingOptions>(options =>
        {
            options.SendExceptionsDetailsToClients = true;
        });

        Configure<AbpAutoMapperOptions>(opt =>
        {
            opt.AddProfile<PLCSProfile>();
        });
        context.Services.AddTransient<StationHub>();
        context.Services.AddTransient<RGVHub>();
        context.Services.AddTransient<FrameHub>();
        Configure<AbpSignalROptions>(options =>
        {
            options.Hubs.Add(
                new HubConfig(
                    typeof(StationHub), //Hub type
                    "/station-hub/route", //Hub route (URL)
                    hubOptions =>
                    {
                        //hubOptions.TransportMaxBufferSize = 51200;
                        //hubOptions.ApplicationMaxBufferSize = 51200;
                        //Additional options
                        hubOptions.LongPolling.PollTimeout = TimeSpan.FromSeconds(30);
                    }
                )
            );
            options.Hubs.Add(
                new HubConfig(
                    typeof(RGVHub),
                    "/rgv-hub/route",
                    hubOptions =>
                    {
                        hubOptions.TransportMaxBufferSize = 51200;
                        hubOptions.ApplicationMaxBufferSize = 51200;
                        hubOptions.LongPolling.PollTimeout = TimeSpan.FromSeconds(30);
                    }
                    ));
            options.Hubs.Add(
                new HubConfig(
                    typeof(FrameHub),
                    "/frame-hub/route",
                    hubOptions =>
                    {
                        hubOptions.LongPolling.PollTimeout = TimeSpan.FromSeconds(30);
                    }
                    ));
        });

        context.Services.AddCors(options =>
        {
            options.AddPolicy("any", builder =>
            {
                builder
                    .WithOrigins(
                        "http://192.168.0.235:8080/",
                        "http://192.168.0.235:8080/"
                    )
                    .WithAbpExposedHeaders()
                    .SetIsOriginAllowedToAllowWildcardSubdomains()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
            });
        });
    }

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        //app.UseRouting();
        app.UseCors("any");
        //app.UseConfiguredEndpoints(endpoints =>
        //{
        //    endpoints.MapHub<StationHub>("/station-hub/route")/*.RequireCors(t => t.SetIsOriginAllowed((host) => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials())*/;
        //});
        //app.UseHttpsRedirection();
    }
}